package com.jiayou.im.controller

import cn.hutool.json.JSONUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.jiayou.im.entity.User
import com.jiayou.im.entity.Userinfo
import com.jiayou.im.service.impl.UserServiceImpl
import com.jiayou.im.service.impl.UserinfoServiceImpl
import com.jiayou.im.vo.Resoult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.concurrent.TimeUnit


/**
 *
 *
 * 前端控制器
 *
 *
 * @author lishuang
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/im/user")
open class UserController {

    @Autowired
    private lateinit var userServiceImpl: UserServiceImpl

    @Autowired
    private lateinit var userInfoServiceImpl: UserinfoServiceImpl

    @Autowired
    private lateinit var stringRedisTemplate: StringRedisTemplate

    //发送邮箱验证码
    @RequestMapping("/sendEmail")
    open fun sendEmail(@RequestParam("email") email: String): Resoult<String> {
        userServiceImpl.sendEmail(email)
        return Resoult.success("邮件发送成功，请尽快验证！")
    }

    //用户注册
    @Transactional
    @PostMapping("register")
    open fun register(user: User, @RequestParam("code") code: String): Resoult<String> {
        if (userServiceImpl.getById(user.username) != null)
            return Resoult(201, "该用用户名已经被注册了喔")

        try {
            val redisCode =
                stringRedisTemplate.opsForValue()[user.email.toString()] ?: return Resoult(201, "无该验证码或该验证码已过期！")

            val obj = JSONUtil.parseObj(redisCode)

            if (obj["code"].toString() == code) {
                stringRedisTemplate.opsForValue().set(user.email.toString(), "", 1, TimeUnit.SECONDS)  //使验证码失效
                return if (userServiceImpl.save(user) && userInfoServiceImpl.save(
                        Userinfo(
                            username = user.username,
                            petname = "苦涩的柠檬",
                            gender = 1,
                            description = "介绍一下自己，让大家认识你把。",
                            img = "head.png"
                        )
                    )
                )
                    Resoult(200, "注册成功！")
                else
                    Resoult(201, "注册失败！")
            } else
                return Resoult(201, "验证码错误！")

        } catch (e: Exception) {
            return Resoult(500, "系统错误！")
        }

    }


    //用户登录
    @RequestMapping("/login")
    open fun login(user: User): Resoult<String> {
        if (user.email == null)
            return if (userServiceImpl.getOne(KtQueryWrapper<User>(user)) != null) {
                Resoult(200, "登陆成功!")
            } else
                Resoult(201, "验证失败！")
        else
            return if (userServiceImpl.getOne(
                    KtQueryWrapper<User>(User::class.java)
                        .eq(User::email, user.email)
                        .eq(User::password, user.password)
                ) != null
            ) {
                Resoult(200, "登陆成功!")
            } else
                Resoult(201, "验证失败！")
    }


}